package vn.com.shoppie.object;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.util.FloatMath;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

@SuppressWarnings("rawtypes")
public class ShopOverlay extends ItemizedOverlay {
	private ArrayList<ShopOverlayItem> mOverlays = new ArrayList<ShopOverlayItem>();
	Context mContext;

	Drawable drawable = null;
	GeoPoint location;
	float radius = 0;

	public ShopOverlay(Context context, Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
		mContext = context;
	}
	public void refreshMarker(int zoom){
		for(ShopOverlayItem item: mOverlays){
			item.refreshDrawable(zoom);
		}
	}
	public int getHeight() throws NullPointerException {
		if (drawable == null || drawable.getBounds() == null)
			throw new NullPointerException();

		int height = drawable.getBounds().height();
		return height;
	}

	public ShopOverlay(Context context, Drawable defaultMarker, GeoPoint location, float radius) {
		super(boundCenterBottom(defaultMarker));
		mContext = context;
		// this.drawable = defaultMarker;
		this.location = location;
		this.radius = radius;
	}

	@Override
	protected boolean onTap(int index) {
		ShopOverlayItem item = mOverlays.get(index);
		if (item.getTapListener() != null) {
			item.getTapListener().onTap(item);
		}
		return true;
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}

	public void addOverlay(ShopOverlayItem overlay) {
		mOverlays.add(overlay);
		populate();
	}

	public ArrayList<ShopOverlayItem> getOverlays() {
		return mOverlays;
	}

	public int getIndexOverlay(ShopOverlayItem item) {
		return mOverlays.indexOf(item);
	}

	public void addOverlays(ArrayList<ShopOverlayItem> overlays) {
		if (overlays != null) {
			mOverlays.addAll(overlays);
		}
		populate();
	}

	public void removeAllOverlay() {
		mOverlays.removeAll(mOverlays);
		populate();
	}

	public void setOverlay(ShopOverlayItem overlay) {
		removeAllOverlay();
		addOverlay(overlay);
	}

	public void setRadius(float radius) {
		this.radius = radius;
	}

	public void setMyLocation(GeoPoint geopoint) {
		this.location = geopoint;
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		if (!shadow) {
			super.draw(canvas, mapView, false);
		}
		if (radius > 0) {
			int height = 12;
			try {
				height = getHeight();
			} catch (NullPointerException ex) {
			}
			;

			Projection projection = mapView.getProjection();

			Point pt = new Point();
			projection.toPixels(location, pt);
			float circleRadius = projection.metersToEquatorPixels(radius) * (1 / FloatMath.cos((float) Math.toRadians(location.getLatitudeE6() / 1E6)));

			Paint innerCirclePaint;

			// fill circle
			innerCirclePaint = new Paint();
			innerCirclePaint.setColor(Color.BLUE);
			innerCirclePaint.setAlpha(25);
			innerCirclePaint.setAntiAlias(true);
			innerCirclePaint.setStyle(Paint.Style.FILL);
			canvas.drawCircle((float) pt.x, (float) pt.y - height, circleRadius, innerCirclePaint);

			// draw circle bound
			innerCirclePaint = new Paint();
			innerCirclePaint.setColor(Color.BLUE);
			innerCirclePaint.setAlpha(50);
			innerCirclePaint.setAntiAlias(true);
			innerCirclePaint.setStyle(Paint.Style.STROKE);
			canvas.drawCircle((float) pt.x, (float) pt.y - height, circleRadius, innerCirclePaint);
		}
	}

}
